import { Component } from '@angular/core';
import { IonicPage,ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../../providers/http/http';
import { StorageProvider } from '../../../providers/storage/storage';
import { DialogProvider } from '../../../providers/dialog/dialog';

/**
 * Generated class for the ModaldepartselectPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-modaldepartselect',
	templateUrl: 'modaldepartselect.html',
})
export class ModaldepartselectPage {

	private selectitem: any;
	private multiselect: any;
	private searchQuery: any;
	private listItems: Array<{
		id: any,
		pId: any,
		name: any,
		subname: any,
		fullName: any,
		searched: any
	}>;
		constructor(
		public viewCtrl: ViewController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
			this.selectitem = this.navParams.get("selectitem"); //页面类型
		this.multiselect = this.navParams.get("multiselect"); //页面类型
	}


	ionViewDidLoad() {
		console.log('ionViewDidLoad Modaldepartselect');
		this.loadData();
	}

	loadData() {
		//初始化
		let params = new Object();
		params['type'] = 2;
		this.http.postJson('treeData', 'officeTree.do', params).then(res => {
			//返回数据之后,进行查询
			let datas = res['data']['list'];
			this.listItems = new Array();
			let group = new Object();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				if (item['id'].length == 32 && item['pId'].length == 32) {
					this.listItems.push({
						id: item['id'],
						pId: item['pId'],
						name: item['name'],
						subname: '',
						fullName: item['fullName'],
						searched: this.selectitem['id'] != item['id'] ? false : true
					});
				} else {
					group[item['id']] = item['name'];
				}
			}
			//部门显示
			for (let i = 0; i < this.listItems.length; i++) {
				let item = this.listItems[i];
				if (group[item['pId']] != undefined) {
					item['subname'] = "(" + group[item['pId']] + ")";
				}
			}
			//最终结果
			this.searchQuery = this.listItems;
		});
	}

	//人物id以u_开头
	getItems(ev: any) {
		//输入关键字
		let val = ev.target.value;
		//查询
		if (val && val.trim() != '') {
			this.searchQuery = this.listItems.filter((item) => {
				return (item['name'].toLowerCase().indexOf(val.toLowerCase()) > -1);
			})
		}else{
			this.searchQuery = this.listItems;
		}
	}

	//选中操作
	doselect(obj, ev: any) {
		//排他处理
		if (ev && !this.multiselect) {
			//对比
			for (let i = 0; i < this.listItems.length; i++) {
				if (obj['id'] != this.listItems[i]['id']) {
					this.listItems[i]['searched'] = false;
				}
			}
			//部门显示
			for (let i = 0; i < this.searchQuery.length; i++) {
				if (obj['id'] != this.searchQuery['id']) {
					this.searchQuery['searched'] = false;
				}
			}
		}
	}
	//关闭选择
	doclose() {
		this.viewCtrl.dismiss();
	}
	//确定选择
	doneselect() {
		//检查选择
		let datas = new Array();
		for (let i = 0; i < this.listItems.length; i++) {
			if (this.listItems[i]['searched'] == true) {
				datas.push(this.listItems[i]);
			}
		}
		//判断选择
		if (datas.length == 0) {
			this.dialog.showAlert('提示', '请选择部门');
		} else {
			let data = new Object();
			data['result'] = datas;
			this.viewCtrl.dismiss(data);
		}
	}
}